﻿using IFRS.Web;
using IFRS_Global;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.DataAccess.DAL;
using IFRS.Models;
using System.Globalization;
using System.Threading;

public partial class IFRS_WIN31_1S : PageBase
{

    #region "---------- Properties -----------"
    private string SortingExpression
    {
        get
        {
            return ViewState["SortingExpression"] != null
                ? Convert.ToString(ViewState["SortingExpression"])
                : string.Empty;
        }
        set { ViewState["SortingExpression"] = value; }
    }
    private SortDirection? SortingDirection
    {
        get
        {
            return ViewState["SortDirection"] != null
                ? (SortDirection?)ViewState["SortDirection"]
                : null;
        }
        set { ViewState["SortDirection"] = value; }
    }
    #endregion Properties

    #region "---------- Member -----------"
    
    private GLAccountPosting glAccountPosting = new GLAccountPosting();
    private Paging paging = new Paging();
    #endregion Member

    #region "---------- Events -----------"
    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        if (!IsPostBack)
        {
            this.MenuTitleBar = Resources.ifrsMenu.WIN31_1S;
            AddClientScript();
            BindDDL();
        }
    }

    protected void gvSearch_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowIndex > -1 && e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton lnkEdit = (LinkButton)e.Row.FindControl("lnkEdit");
            LinkButton lnkDel = (LinkButton)e.Row.FindControl("lnkDelete");
            DataRowView drv = (e.Row.DataItem as DataRowView);
            e.Row.SetRowAttribute("GLAccountPostingID");
            e.Row.SetRowAttribute("GLGroupID");
            e.Row.SetLinkAttribute("lnkCopy", "GLAccountPostingID");
            e.Row.SetLinkAttribute("lnkCopy", "GLGroupID");
            e.Row.SetLinkAttribute("lnkView", "GLAccountPostingID");
            e.Row.SetLinkAttribute("lnkView", "GLGroupID");
            e.Row.SetLinkAttribute("lnkEdit", "GLAccountPostingID");
            e.Row.SetLinkAttribute("lnkEdit", "GLGroupID");
            e.Row.SetLinkAttribute("lnkDelete", "GLAccountPostingID");
            e.Row.SetLinkAttribute("lnkDelete", "GLGroupID");

        }
    }
    protected void gvSearch_Sorting(object sender, GridViewSortEventArgs e)
    {
        paging = Page.LoadViewState(paging, ViewState);

        SortingExpression = e.SortExpression.ToUpper();
        SortingDirection = SortingDirection == SortDirection.Ascending ? SortDirection.Descending : SortDirection.Ascending;

        paging.Sorting = e.Sorting(SortingExpression, SortingDirection);
        Page.SaveViewState(paging, ViewState);

        GetCustomersPageWise(paging.PageIndex);
    }
    protected void lnkDeleteItems_Click(object sender, EventArgs e)
    {
        try
        {
            int iID = Convert.ToInt32(((LinkButton)sender).Attributes["GLAccountPostingID"]);
            GLAccountPosting GLAccountPostingDel = new GLAccountPosting();
            GLAccountPostingDel.GLAccountPostingID = iID;
            GLAccountPostingDel.UpdatedDate = DateTime.Now;
            GLAccountPostingDel.UpdatedBy = SessionManager.IFRS_EMPLOYEE_ID;
            GLAccountPostingDel.UpdatedBranch = SessionManager.IFRS_BRANCH;
            GLAccountPostingDel.UpdatedCostCenter = SessionManager.IFRS_COST_CENTER;

            int result = new SPR_GLACCOUNTPOSTING().DELETE(GLAccountPostingDel);
            if (result > 0)
            {
                Utility.AlertMessage(Page, Resources.ifrsResource.DeleteCompleted);
                BindSearchData();
            }
            else
            {
                Utility.AlertMessage(Page, Resources.ifrsResource.DeleteFailed);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    //protected void lnkCopyItems_Click(object sender, EventArgs e)
    //{
    //    string strKey = ((LinkButton)sender).Attributes["GLAccountPostingID"];
    //    int strKeyObj = ((LinkButton)sender).Attributes["GLGroupID"].AsInt32();
    //    Response.Redirect(ResolveUrl(string.Format(GetObject(strKeyObj) + "_2E.aspx?&id={0}", strKey)));
    //}
    protected void lnkEditItems_Click(object sender, EventArgs e)
    {
        string strKey = ((LinkButton)sender).Attributes["GLAccountPostingID"];
        int strKeyObj = ((LinkButton)sender).Attributes["GLGroupID"].AsInt32();
        Response.Redirect(ResolveUrl(string.Format(GetObject(strKeyObj) + "_2E.aspx?&id={0}", strKey)));
    }
    protected void lnkViewItems_Click(object sender, EventArgs e)
    {
        string strKey = ((LinkButton)sender).Attributes["GLAccountPostingID"];
        int strKeyObj = ((LinkButton)sender).Attributes["GLGroupID"].AsInt32();
        Response.Redirect(ResolveUrl(string.Format(GetObject(strKeyObj) + "_3V.aspx?&id={0}", strKey)));
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN31_4A.aspx");
    }
    protected void btnSEARCH_Click(object sender, EventArgs e)
    {
        BindSearchData();
    }
    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl(Request.Url.AbsoluteUri));
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Index/Index.aspx");
    }
    protected void Page_Changed(object sender, EventArgs e)
    {
        int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
        this.GetCustomersPageWise(pageIndex);
    }
    #endregion

    #region "---------- Methods ----------"
    private void ViewMenu()
    {
        this.MenuTitleBar = Resources.ifrsMenu.WIN31_1S;
    }

    private void AddClientScript()
    {
        btnCANCEL.Attributes["OnClick"] = "if(!confirm(\"" + Resources.ifrsResource.ConfirmCancel + "\")) return false;";
        btnGLAccountCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/GLAccount_GL.aspx?&PopUp=GL_ACCOUNT") + "','700','550');");
        btnAdd.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/GLGroup_GL.aspx") + "','550','250');");
     
        txtGLAccountCode.AddControl(hidGLAccount.ClientID);
        txtGLAccountCode.AddControl(txtGLAccountCode.ClientID);
        txtGLAccountCode.AddControl(txtGLAccountName.ClientID);
        txtGLAccountCode.ControlSearch = txtGLAccountCode.ClientID;
        txtGLAccountCode.SearchKey = "GLAccount_oGLAccountCode_oGLAccountName_GL";
    }

    private void BindSearchData()
    {
        this.GetCustomersPageWise(1);
    }
    private void PopulatePager(int recordCount, int currentPage)
    {
        double dblPageCount = (double)((decimal)recordCount / 10);
        int pageCount = (int)Math.Ceiling(dblPageCount);

        List<ListItem> pages = new List<ListItem>();
        if (pageCount > 0)
        {
            pages.Add(new ListItem("First", "1", currentPage > 1));
            for (int i = 1; i <= pageCount; i++)
            {
                pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
            }

            pages.Add(new ListItem("Last", pageCount.ToString(), currentPage < pageCount));
        }

        repPager.DataSource = pages;
        repPager.DataBind();
    }

    private void GetCustomersPageWise(int pageIndex)
    {

        paging = Page.LoadViewState(paging, ViewState) ?? new Paging();
        paging.Sorting = paging.Sorting == null ? "" : paging.Sorting;
        paging.PageSize = gvSearch.PageSize;
        paging.PageIndex = pageIndex;
        glAccountPosting.GLGroupID = ddlGLGroup.SelectedValue.AsIntIsnull();
        glAccountPosting.GLAccountID = hidGLAccount.Value.AsIntIsnull();
        int recordCount = 0;
        SessionManager.IFRS_ALLBRANCH = true;
        var dt = new SPR_GLACCOUNTPOSTING().SEARCH_ALL(glAccountPosting, paging, SessionManager.IFRS_ALLBRANCH, SessionManager.IFRS_COST_CENTER, ref recordCount);

        gvSearch.DataSource = dt;
        gvSearch.DataBind();
        this.PopulatePager(recordCount, pageIndex);

        Page.SaveViewState(paging, ViewState);
    }
    private void BindDDL()
    {
        ddlGLGroup.SetData(DDL_TYPE.GLGroup, "", true);
    }

    private string GetObject(int iObjID)
    {
        string ObjPart = string.Empty;
        switch (iObjID)
        {
            case 1: ObjPart = "~/IFRS/WIN31_GL_DEPO";
                break;
            case 2: ObjPart = "~/IFRS/WIN31_GL_LN";
                break;
            case 3: ObjPart = "~/IFRS/WIN31_GL_TF";
                break;
            case 4: ObjPart = "~/IFRS/WIN31_GL_INS_LN";
                break;
            case 5: ObjPart = "~/IFRS/WIN31_GL_LOT";
                break;
            case 6: ObjPart = "~/IFRS/WIN31_GL_TR";
                break;
            case 7: ObjPart = "~/IFRS/WIN31_GL_ISL";
                break;
            case 8: ObjPart = "~/IFRS/WIN31_GL_INV";
                break;
        }
        return ObjPart;
    }
    #endregion
}